package com.ptszyxx.popose.common.weiget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.adapter.CommonPicAdapter;
import com.ysg.http.data.entity.dynamic.DynamicResult;
import com.ysg.utils.YCollectionUtil;
import com.ysg.utils.YImageUtil;
import com.ysg.utils.YRecyclerViewUtil;
import com.ysg.widget.pic.YPicUtils;

import java.util.List;

/**
 * 标题栏
 */
public class DynamicImage extends LinearLayout {
    private View parent;
    private CardView cv_pic;
    private ImageView iv_pic;
    private TextView tv_duration;
    private RecyclerView recyclerView;

    public DynamicImage(Context context) {
        super(context);
    }

    public DynamicImage(Context context, AttributeSet attrs) {
        super(context, attrs);

        View view = View.inflate(context, R.layout.widget_dynamic_image, this);
        parent = view.findViewById(R.id.parent);
        cv_pic = view.findViewById(R.id.cv_pic);
        iv_pic = view.findViewById(R.id.iv_pic);
        tv_duration = view.findViewById(R.id.tv_duration);
        recyclerView = view.findViewById(R.id.recyclerView);


    }

    public void setData(DynamicResult data){
        List<String> imageList = data.getImageList();
        if (YCollectionUtil.isNotEmpty(imageList)){
            parent.setVisibility(View.VISIBLE);
            if (1 == imageList.size()){
                recyclerView.setVisibility(View.GONE);
                cv_pic.setVisibility(View.VISIBLE);
                YImageUtil.show(iv_pic, imageList.get(0));

                cv_pic.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        YPicUtils.getInstance().zoomPic(getContext(), imageList, 0);
                    }
                });
            }else {
                recyclerView.setVisibility(View.VISIBLE);
                cv_pic.setVisibility(View.GONE);
                CommonPicAdapter adapter = new CommonPicAdapter();
                YRecyclerViewUtil.initGrid(recyclerView.getContext(), recyclerView, adapter, 3);
                adapter.setList(imageList);

                adapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                        YPicUtils.getInstance().zoomPic(getContext(), imageList, position);
                    }
                });
            }
        }else{
            parent.setVisibility(View.GONE);
        }
    }






}
